use serde::{Deserialize, Serialize};
// src-tauri/src/memory_monitor.rs
use sysinfo::{System, SystemExt};

pub struct MemoryMonitor {
    system: System,
    max_memory_mb: u64,
}

impl MemoryMonitor {
    pub fn new(max_memory_mb: u64) -> Self {
        Self {
            system: System::new_all(),
            max_memory_mb,
        }
    }

    pub fn should_clean_cache(&mut self) -> bool {
        self.system.refresh_memory();
        let used_memory_mb = self.system.used_memory() / 1024 / 1024;
        
        used_memory_mb > self.max_memory_mb * 80 / 100 // 超过80%时清理
    }

    pub fn get_memory_usage(&mut self) -> MemoryUsage {
        self.system.refresh_memory();
        
        MemoryUsage {
            used_mb: self.system.used_memory() / 1024 / 1024,
            total_mb: self.system.total_memory() / 1024 / 1024,
            cache_entries: 0, // 需要从各个缓存系统获取
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct MemoryUsage {
    pub used_mb: u64,
    pub total_mb: u64,
    pub cache_entries: usize,
}